﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
#if WINDOWS
using Microsoft.Xna.Framework.Storage;
#endif
using Microsoft.Xna.Framework.Media;
using Neat;
using Neat.MenuSystem;
using Neat.EasyMenus;
using Neat.GUI;
using Neat.Mathematics;
using Neat.Graphics;
using System.IO;

namespace RiverRaidX
{
    public class MainMenuX : Menu
    {
        public MainMenuX(NeatGame G)
            : base(G)
        {
        }

        RenderTarget2D gameTarget;
        Effect effect;
        float tintPhase;
        float tintChangeRate = 0.01f;
        Vector4 color1 = Color.White.ToVector4();
        Vector4 color2 = Color.Red.ToVector4();
        Vector4 tint;
        float alpha = 0;
        float alphaV = 0.005f;

        public override void LoadContent()
        {
            if (game.GraphicsDevice.DisplayMode.Height >= 1080)
            {
                game.LoadFont(@"Fonts\MenuFontBig","menufont");
            }
            gameTarget = new RenderTarget2D(game.GraphicsDevice,
                game.GraphicsDevice.PresentationParameters.BackBufferWidth, game.GraphicsDevice.PresentationParameters.BackBufferHeight,
                false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
            effect = game.GetEffect("ripple");
            base.LoadContent();
        }

        public override void CreateMenu()
        {
            System.AddItem("Play", true, null, "g_start");
            System.AddItem("Options", true, null, "sh optionsmenu");
#if KINECT
            System.AddItem("Calibrate Kinect", true, null, "sh kinect");
#endif
            System.AddItem("Credits", true, null, "sh creditsscreen");
            System.AddItem("Quit", true, null, "sh quitconfirm");
            System.GetLastMenuItem().Forecolor = Color.Red;

            if (game.GraphicsDevice.DisplayMode.Height >= 1080)
                System.ItemsOffset = new Vector2(0, 100);
            else
                System.ItemsOffset = new Vector2(0, 75);
            Activate();
            base.CreateMenu();
        }
        
        public override void Activate()
        {
            alpha = 0;
            /*gameTarget = new RenderTarget2D(game.GraphicsDevice,
                game.GraphicsDevice.PresentationParameters.BackBufferWidth, game.GraphicsDevice.PresentationParameters.BackBufferHeight,
                false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);*/
            game.Console.Run("rr_noplayer true");
            game.Console.Run("e_framerate 60");
            
            base.Activate();

            System.Position += new Vector2(0, 75);
            if (game.GraphicsDevice.DisplayMode.Height < 1080) 
                System.Position += new Vector2(0, 25);

            if (game.PreviousScreen == "gameoverscreen" || game.PreviousScreen == "ingamemenu")
            {
                MediaPlayer.Play(game.GetSong("song1"));
            }
            game.ShowMouse = true;
        }

        public override void Deactivate(string nextScreen)
        {
            effect.Parameters["uvShift"].SetValue(Vector2.Zero);
            effect.Parameters["tint"].SetValue(Vector4.One);
            game.Console.Run("e_framerate 30");
            base.Deactivate(nextScreen);
        }
        public override void Behave(GameTime gameTime)
        {
            alpha += alphaV;
            tintPhase += tintChangeRate;
            tint.X = MathHelper.Lerp(color1.X, color2.X, tintPhase);
            tint.Y = MathHelper.Lerp(color1.Y, color2.Y, tintPhase) * 0.5f;
            tint.Z = MathHelper.Lerp(color1.Z, color2.Z, tintPhase);
            tint.W = 1.0f;
            if (tintPhase > 1.0f)
            {
                tintPhase = 0.0f;
                color1 = color2;
                color2 = GraphicsHelper.GetRandomColor().ToVector4();
            }
            game.Screens["game"].Update(gameTime);
            base.Behave(gameTime);
        }
        public override void Render(GameTime gameTime)
        {
            game.SpriteBatch.End();

            game.GraphicsDevice.SetRenderTarget(gameTarget);
            game.GraphicsDevice.Clear(new Color(tint));
            game.SpriteBatch.Begin();
            game.Screens["game"].Render(gameTime);
            game.SpriteBatch.End();
            
            game.GraphicsDevice.SetRenderTarget(game.DefaultTarget);
            effect.Parameters["phase"].SetValue(MathHelper.ToRadians(Frame));
            effect.Parameters["tint"].SetValue(tint);
            effect.Parameters["uvShift"].SetValue(new Vector2((float)Math.Cos(MathHelper.ToRadians(Frame*0.1f)), (float)Math.Sin(MathHelper.ToRadians(Frame*0.1f))));
            game.GraphicsDevice.Clear(new Color(tint));
            game.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, DepthStencilState.None, null, effect);
            game.SpriteBatch.Draw(gameTarget, new Rectangle(0,0, game.GameWidth, game.GameHeight), Color.White);
            game.RestartBatch();

            game.SpriteBatch.Draw(game.GetTexture("menuBackground"), new Rectangle(0, 0, game.GameWidth, game.GameHeight), Color.White);
            base.Render(gameTime);

            if (alpha < 1)
            {
                game.SpriteBatch.Draw(game.GetTexture("solid"), new Rectangle(0, 0, game.GameWidth, game.GameHeight), GraphicsHelper.GetColorWithAlpha(Color.Black, 1-alpha));
            }
        }
    }
}
